<template>
	<view>
		<view class="My-fankui">
			<textarea value="" placeholder="请输入反馈内容" v-model="textareaTxt"/>
			<view class="sbmbit-btn" @click="submits">
				提交
			</view>
		</view>
		
		<!-- 反馈组件 -->
		<HMmessages ref="HMmessages" @complete="HMmessages = $refs.HMmessages" @clickMessage="clickMessage"></HMmessages>
	</view>
</template>

<script>
	var db = wx.cloud.database()
	var users = db.collection('users')
	
	//引入时间模块
	var util = require('../../components/timer.js');
	var time = util.formatTime(new Date());
	
	//顶部消息提示插件
	 import HMmessages from "@/HMtolel/HM-messages/HM-messages.vue"
	export default {
		data() {
			return {
				textareaTxt:'',//用户输入意见
				
			}
		},
		components:{
			HMmessages
		},
		methods: {
			//判断是否登录
			submits(){
				if(this.textareaTxt ==''){
					this.HMmessages.show("请输入反馈内容！",{icon:'error',fontColor:'#000000',background:'#f40000',iconColor:'#000000'})
				}else{
					this.isUserinfo()
				}
			},
			
			isUserinfo(){
				users.get().then(res=>{
					console.log(res)
					if(res.data.length!=0){
						console.log('用户已经登录')
						//上产用户的信息，用户的反馈
						this.uploadText(res.data)
						
					}else{
						// console.log('先登录')
						this.HMmessages.show("请先登录才能反馈哦！",{icon:'error',fontColor:'#000000',background:'#f40000',iconColor:'#000000'})
						
					}
				}).catch(err=>{
					console.log(err)
				})
			},
			
			//开始上传
			uploadText(res){
				// console.log(this.textareaTxt)
				// console.log(time)
				// console.log(res)
				let dataTxt = {
					textareaTxt:this.textareaTxt,
					time:time,
					user:res
				}
				const db = wx.cloud.database()
				const users = db.collection('feedback').add({
					data:dataTxt
				}).then(res=>{
					console.log(res)
					this.HMmessages.show("感谢您的反馈",{icon:'success',fontColor:'#000000',background:'#55ff00',iconColor:'#000000'})
					//反馈成功后一秒后跳转
					const timer = setTimeout(function(){
						wx.navigateBack({
						 		delta:1
							})
					},1000)
				}).catch(err=>{
					console.log(err)
				})
				
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #eaeaea;
	}
	.My-fankui{
		display: flex;
		flex-direction: column;
		align-items: center;
		textarea{
			box-sizing: border-box;
			padding: 28upx;
			width: 100%;
			height: 350upx;
			background-color: #ffffff;
			border-radius: 20upx;
		}
		.sbmbit-btn{
			border-radius: 15upx;
			border: 1px solid #5500ff;
			padding: 15upx 50upx;
			box-shadow: 0 0 10upx rgba(85, 0, 255, 0.5);
			margin-top: 30upx;
		}
	}
</style>
